package com.lintf.common.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @ system:Mac
 * @ author:lin tingfeng
 * @ data:2023/12/7 14:46
 * @ package_name:com.lintf.common.utils
 * @ project_name:MobileProject
 * @ desc:
 */
public class BannerGenerator {
    public static void main(String[] args) {
        String text = "Hello, World!";
        int width = 800;
        int height = 200;
        String fontFamily = "Arial";
        int fontSize = 48;
        Color fontColor = Color.BLACK;
        Color backgroundColor = Color.WHITE;

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(backgroundColor);
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(fontColor);
        Font font = new Font(fontFamily, Font.PLAIN, fontSize);
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics();
        int textWidth = fontMetrics.stringWidth(text);
        int textHeight = fontMetrics.getHeight();
        int x = (width - textWidth) / 2;
        int y = (height - textHeight) / 2 + fontMetrics.getAscent();
        graphics.drawString(text, x, y);

        try {
            ImageIO.write(image, "png", new File("banner.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
